package com.example.baiguangsystem.config;

import com.example.baiguangsystem.entity.Author;
import com.example.baiguangsystem.vo.ResultType;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 作者aop配置类，专门用于将作者类的date日期属性转换为string类型并且赋值给authorDateFormat字段
 *
 * @Author: 27641
 * @ProjectName: BaiGuangSystem
 * @Date: 2022/02/06/14:22
 * @noinspection SpellCheckingInspection
 */
@Component
@Aspect
public class AuthorDateTransformAop {

    /**
     * 切入点
     */
    @Pointcut(value = "execution(* com.example.baiguangsystem.constroller.AuthorController.*(..))")
    public void MyPointcut() {
    }

    /**
     * 后置增强
     *
     * @param result 作者类控制器的方法执行返回结果,用于对数据进行二次封装
     */
    @AfterReturning(value = "MyPointcut()", returning = "result")
    public void postposition(Object result) {
        if ((result != null) && (result instanceof ResultType) && (((ResultType<?>) result).getData()
                instanceof Author)) {
            Author author = (Author) ((ResultType<?>) result).getData();
            if (author.getAuthorDate() != null) {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                author.setAuthorDateFormat(dateFormat.format(author.getAuthorDate()));
                ((ResultType<?>) result).setMsg(((ResultType<?>) result).getMsg() + "作者Aop类对返回结果进行了增强！");
            }
        }
    }
}
